<?php

class Picks extends Controller {

    function __construct() {
        parent::__construct();

        if (!UserAccess::checkLoggedin()) { //If user is not logged in sent back to login page
            header('location: ' . URL . 'login');
            exit;
        }

//        $this->view->js[0] = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
//        $this->view->js[1] = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js';
        $this->view->js_desktop[0] = URL . 'public/js/jquery_min.js';
        $this->view->js_desktop[1] = URL . 'public/js/jquery_ui_min.js';
        $this->view->js_desktop[2] = URL . 'public/js/tabs.js';
        $this->view->js_desktop[3] = URL . 'public/js/sendmessage.js';
        $this->view->js_desktop[4] = URL . 'public/js/picklist.js';
        $this->view->js_desktop[5] = URL . 'public/js/slide.js';
        $this->view->js_desktop[6] = URL . 'public/js/equalheights.js';

        $this->view->css_desktop[0] = URL . 'public/css/default.css';
        $this->view->css_desktop[1] = URL . 'public/css/picklist.css';
        $this->view->css_desktop[2] = URL . 'public/css/slide.css';

        $this->view->css_mobile[0] = 'http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css';
        $this->view->js_mobile[0] = URL . 'public/js/jquery_min_1_5_2.js';
        $this->view->js_mobile[1] = URL . 'public/js/jquery_mobile.js';
        $this->view->js_mobile[2] = URL . 'public/js/m_picks.js';
    }

    function index() {
        if (UserAccess::GetMobileViewer())
            $this->view->render('mobile/picks/index');
        else
            $this->view->render('desktop/picks/index');
    }

    function tabs($index) {
        echo "Page " . $index;
    }

    function Sortable() {

        $Round = $_POST['Round'];
        $Priority = $_POST['Round_' . $Round . '_Priority'];
        $memberId = $_SESSION['memberId'];

        $this->model->SetPriorityPointsForRound($Priority, $Round, $memberId);
    }

    function SetPicks() {   //Team_A_PickId_xxxx
        $PickedText = $_POST['Picked'];
        $Picked = explode('_', $PickedText);

        $Round = $Picked[1];
        if (strcmp($Picked[3], 'A') == 0)
            $Result = 'Team A wins';
        else
            $Result = 'Team B wins';

        $PickedID = $Picked[5];
        $memberId = $_SESSION['memberId'];

//        echo 'Result: '.$Result.'  , PickedID: '.$PickedID.' Round: '.$Round.PHP_EOL;
        $this->model->SetPicks($Round, $PickedID, $memberId, $Result);
    }

    function xhrsendmessage() {
        $this->model->xhrsendmessage();
    }

    function xhrgetmessages($NrOfMessages = 10) {
        $this->model->xhrgetmessages($NrOfMessages);
    }

    //Set Picks round used in mobile setup
    function SetPicksRound($RoundNr) {

        $RoundNr = explode('_', $RoundNr);
        $RoundNr = $RoundNr[1];

        $_SESSION['PicksRoundView'] = $RoundNr;

        $sth = $this->model->GetRoundInfo($RoundNr);
        $data = $sth->fetch();
        
        $RoundStatus = $data['roundStatus'];
        
        echo 'RoundStatus: ' . $RoundStatus;
        
        $sth = $this->model->GetPicksInfo($RoundNr, $_SESSION['memberId']);

        //Echo text to put in HTML doc.
        echo '<ul id = "MatchsInRound" data-role="listview" data-theme="g">' . PHP_EOL;

        while ($data = $sth->fetch()) {

            $PicksResult = $data['picksResult'];
            $NameTeamA = $data['nameTeamA'];
            $DisplayNameTeamA = $data['displayNameTeamA'];
            $ImageTeamA = $data['imageNameTeamA'];
            $NameTeamB = $data['nameTeamB'];
            $DisplayNameTeamB = $data['displayNameTeamB'];
            $ImageTeamB = $data['imageNameTeamB'];
            $pickId = $data['pickId'];
            
            echo '<li><a href="'.URL.'picks/getpicksinfo/'.$pickId.'">'.$DisplayNameTeamA.' VS '.$DisplayNameTeamB.'</a></li>'.PHP_EOL;
        }
        echo '</ul>' . PHP_EOL;
    }
    
    function GetPicksInfo($RoundNr) {
        echo 'TEST';
    }
    

}